package com.smartscreen.attendancemanage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartscreen.attendancemanage.entity.AttendanceRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface AttendanceRecordMapper extends BaseMapper<AttendanceRecord> {

    // 移除了与student表的JOIN操作，只查询考勤记录
    @Select("SELECT ar.*, rule.name as rule_name " +
            "FROM attendance_record ar " +
            "LEFT JOIN attendance_rule rule ON ar.rule_id = rule.rule_id " +
            "WHERE ar.student_id = #{studentId} " +
            "AND DATE(ar.check_in_time) BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY ar.check_in_time DESC")
    List<AttendanceRecord> getStudentAttendanceRecords(@Param("studentId") String studentId,
                                                       @Param("startDate") LocalDate startDate,
                                                       @Param("endDate") LocalDate endDate);

    // 移除了与student表的JOIN操作，改为通过学生ID列表查询
    @Select("SELECT COUNT(*) as total_records, " +
            "SUM(CASE WHEN ar.status = 'normal' THEN 1 ELSE 0 END) as normal_count, " +
            "SUM(CASE WHEN ar.status = 'late' THEN 1 ELSE 0 END) as late_count, " +
            "SUM(CASE WHEN ar.status = 'absent' THEN 1 ELSE 0 END) as absent_count " +
            "FROM attendance_record ar " +
            "WHERE ar.student_id IN (${studentIds}) " +
            "AND DATE(ar.check_in_time) BETWEEN #{startDate} AND #{endDate}")
    Map<String, Object> getClassAttendanceStatisticsByStudentIds(@Param("studentIds") String studentIds,
                                                                @Param("startDate") LocalDate startDate,
                                                                @Param("endDate") LocalDate endDate);
}